#include "usart.h"

void usart_init()
{
    GPIO_InitTypeDef GPIO_InitStruct;
    USART_InitTypeDef USART_InitStruct;
    NVIC_InitTypeDef NVIC_InitStruct;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStruct.GPIO_Speed = GPIO_High_Speed;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA, &GPIO_InitStruct);

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);

    USART_InitStruct.USART_BaudRate = 115200;
    USART_InitStruct.USART_WordLength = USART_WordLength_8b;
    USART_InitStruct.USART_StopBits = USART_StopBits_1;
    USART_InitStruct.USART_Parity = USART_Parity_No;
    USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
    USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_Init(USART1, &USART_InitStruct);

    NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x00;
    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x00;
    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStruct);
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

    USART_Cmd(USART1, ENABLE);
}

int fputc(int ch, FILE *f)
{
    USART_SendData(USART1, (uint8_t)ch);
    while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
        ;
    return ch;
}

#define RX_BUFFER_SIZE 5
char rx_buffer[RX_BUFFER_SIZE]; // 接收缓冲区
uint8_t rx_index = 0;           // 接收缓冲区索引

void USART1_IRQHandler(void)
{
    if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
    {
        char received_char = USART_ReceiveData(USART1);
        USART_SendData(USART1, received_char);

        // 将字符添加到缓冲区
        if (rx_index < RX_BUFFER_SIZE - 1)
        {
            rx_buffer[rx_index++] = received_char;
            // rx_buffer[rx_index] = '\0';
        }

        if (rx_index == RX_BUFFER_SIZE - 1)
        {
            rx_buffer[rx_index] = '\0'; // 添加字符串结束符
            if (if_password_correct == 0)
            {
                if (strcmp(rx_buffer, "3214") == 0)
                {
                    if_password_correct = 1;
                    printf("password correct\r\n");
                }
                else
                {
                    if_password_correct = 0;
                    printf("password incorrect\r\n");
                }
            }
            else
            {
                if (led_water_mode == 0)
                {
                    if (strcmp(rx_buffer, "LED1") == 0)
                    {
                        GPIO_ToggleBits(LED_GROUP, LED1_PIN);
                        printf("LED1 toggled\r\n");
                    }
                    else if (strcmp(rx_buffer, "LED2") == 0)
                    {
                        GPIO_ToggleBits(LED_GROUP, LED2_PIN);
                        printf("LED2 toggled\r\n");
                    }
                    else if (strcmp(rx_buffer, "LED3") == 0)
                    {
                        GPIO_ToggleBits(LED_GROUP, LED3_PIN);
                        printf("LED3 toggled\r\n");
                    }
                    else if (strcmp(rx_buffer, "LED4") == 0)
                    {
                        GPIO_ToggleBits(LED_GROUP, LED4_PIN);
                        printf("LED4 toggled\r\n");
                    }
                    else
                    {
                        printf("unknown command\r\n");
                    }
                }
            }
            rx_index = 0;
            memset(rx_buffer, 0, RX_BUFFER_SIZE);
        }
        USART_ClearITPendingBit(USART1, USART_IT_RXNE);
    }
}
